package com.baseDemo.web.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;
import org.springframework.format.annotation.DateTimeFormat;

import com.share.date.JsonDateSerializer;
/**
 * 用户数据库持久化类 
 * @author xianwei
 *
 */
@Entity
@Table(name = "T_UserEntity")
public class UserEntity  implements Serializable {

	private static final long serialVersionUID = 45414933218087187L;
	
	private String id;
	private String userName;
	private String loginID;
	private String password;
	@Column(name="active")
	@Type(type="yes_no")
	private boolean active;
	@Temporal(TemporalType.TIMESTAMP)
	@Column(length = 25)
	private Date createDate  = new Date() ;
	
	private Set<RoleEntity> roles = new HashSet<RoleEntity>();
	
	private Set<AuthorityEntity> authorities = new HashSet<AuthorityEntity>();
	@Id
	@GeneratedValue(generator = "system-uuid")
	@GenericGenerator(name = "system-uuid", strategy = "org.hibernate.id.UUIDGenerator")
	@Column(length = 50)
	public String getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	@Column(length = 50)
	public String getUserName() {
		return userName;
	}
	/**
	 * @param userName the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	@Column(length = 50)
	public String getLoginID() {
		return loginID;
	}
	/**
	 * @param loginID the loginID to set
	 */
	public void setLoginID(String loginID) {
		this.loginID = loginID;
	}
	
	@Column(length = 50)
	public String getPassword() {
		return password;
	}
	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	/**
	 * @return the active
	 */
	public boolean isActive() {
		return active;
	}
	/**
	 * @param active the active to set
	 */
	public void setActive(boolean active) {
		this.active = active;
	}
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")  
	@JsonSerialize(using=JsonDateSerializer.class)
	public Date getCreateDate() {
		return createDate;
	}
	/**
	 * @param createDate the createDate to set
	 */
	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE},mappedBy = "users",fetch = FetchType.LAZY)
	@JsonIgnore
	public Set<RoleEntity> getRoles() {
		return roles;
	}
	public void setRoles(Set<RoleEntity> roles) {
		this.roles = roles;
	}
	@Override
	public boolean equals(Object obj) {
		if(((UserEntity)obj).getId().equals(this.getId())){
			return true;
		}else{
			return false;
		}
	}
	@Transient
	public Set<AuthorityEntity> getAuthorities() {
		return authorities;
	}
	public void setAuthorities(Set<AuthorityEntity> authorities) {
		this.authorities = authorities;
	}
}
